package com.example.calculator;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.nfc.Tag;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.ViewManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_calculator);
        //从id找到控件
        textView = (TextView) findViewById(R.id.textView);
        textView.setMovementMethod(new ScrollingMovementMethod());
        findViewById(R.id.button0).setOnClickListener(this);
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        findViewById(R.id.button4).setOnClickListener(this);
        findViewById(R.id.button5).setOnClickListener(this);
        findViewById(R.id.button6).setOnClickListener(this);
        findViewById(R.id.button7).setOnClickListener(this);
        findViewById(R.id.button8).setOnClickListener(this);
        findViewById(R.id.button9).setOnClickListener(this);
        findViewById(R.id.sqrt).setOnClickListener(this);
        findViewById(R.id.square).setOnClickListener(this);
        findViewById(R.id.add).setOnClickListener(this);
        findViewById(R.id.division).setOnClickListener(this);
        findViewById(R.id.multiply).setOnClickListener(this);
        findViewById(R.id.minus).setOnClickListener(this);
        findViewById(R.id.equal).setOnClickListener(this);
        findViewById(R.id.ce).setOnClickListener(this);
        findViewById(R.id.c).setOnClickListener(this);
        findViewById(R.id.dot).setOnClickListener(this);
        findViewById(R.id.sin).setOnClickListener(this);
        findViewById(R.id.cos).setOnClickListener(this);
        findViewById(R.id.tan).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int resid = v.getId();
        String inputText;
        if (resid == R.id.sqrt) {//开根
            inputText = "√";
        } else {
            inputText = (((TextView) v).getText().toString());
        }
        if (resid == R.id.square) {//平方
            inputText = "^2";
        }
        Log.d("MyClickListener", "resid=" + resid + ",inputText=" + inputText);
        if (resid == R.id.ce) {  //ce按键清空
            if (clear("")) {
                Toast.makeText(getApplicationContext(), "已清空", Toast.LENGTH_SHORT).show();
            }
        } else if (resid == R.id.c) {// c按键消去最后一个字符
            if (operator.equals("")) {
                if (firstNum.length() == 1) {
                    firstNum = "";   //归零
                } else if (firstNum.length() > 1) {
                    firstNum = firstNum.substring(0, firstNum.length() - 1);//去掉最后一个运算符
                } else {
                    Toast.makeText(getApplicationContext(), "别清了，再清人都傻了", Toast.LENGTH_SHORT).show();//为暴躁清零的人量身定做
                    return;
                }
                showText = firstNum;
                textView.setText(showText);
            } else {
                if (nextNum.length() == 1) {
                    nextNum = "";

                } else if (nextNum.length() > 0) {
                    nextNum = nextNum.substring(0, nextNum.length() - 1);
                } else {
                    Toast.makeText(getApplicationContext(), "别清了，再清人都傻了", Toast.LENGTH_SHORT).show();
                    return;//对数字进行同样操作
                }
                showText = showText.substring(0, showText.length() - 1);
                textView.setText(showText);
            }
        } else if (resid == R.id.equal) {//等于操作
            if (operator.length() == 0 || operator.equals("=")) {
                Toast.makeText(getApplicationContext(), "没有运算符啊，老哥！", Toast.LENGTH_SHORT).show();
                return;
            } else if (nextNum.length() == 0) {
                Toast.makeText(getApplicationContext(), "没有数字啊，老哥！", Toast.LENGTH_SHORT).show();
                return;
            }
            if (calculate()) {
                operator = inputText;
                showText = showText + "=" + result;
                textView.setText(showText);//显示结果
            }

        } else if (resid == R.id.add || resid == R.id.minus || resid == R.id.multiply || resid == R.id.division) {
            //运算符操作
            if (firstNum.length() <= 0) {
                Toast.makeText(getApplicationContext(), "请输入数字", Toast.LENGTH_SHORT).show();
                return;
            }
            if (operator.length() == 0 || operator.equals("=") || operator.equals("√") || operator.equals("^2")) {
                operator = inputText;
                showText = showText + operator;
                textView.setText(showText);//直接显示结果
            } else {
                Toast.makeText(getApplicationContext(), "请输入数字", Toast.LENGTH_SHORT).show();
            }
        } else if (resid == R.id.sqrt) {//开根
            if (firstNum.length() == 0) {
                Toast.makeText(getApplicationContext(), "请输入数字", Toast.LENGTH_SHORT).show();
                return;
            } else if (Double.parseDouble(firstNum) < 0) {
                Toast.makeText(getApplicationContext(), "根号里面不能是负数（虚数开根我还不会）", Toast.LENGTH_SHORT).show();
                return;
            }
            result = String.valueOf(Math.sqrt(Double.parseDouble(firstNum)));//得出结果并传递
            firstNum = result;
            nextNum = "";
            operator = inputText;
            showText = showText + "√=" + result;
            textView.setText(showText);
            clearData();//归零
            Log.d("MyOnClick", "result=" + result + ",firstNum=" + firstNum + ",operator=" + operator);
        } else if (resid == R.id.square) {//平方
            if (firstNum.length() == 0) {
                Toast.makeText(getApplicationContext(), "请输入数字", Toast.LENGTH_SHORT).show();
                return;
            }
            result = String.valueOf(Math.pow(Double.parseDouble(firstNum), 2));
            firstNum = result;
            nextNum = "";
            operator = inputText;
            showText = showText + "^2=" + result;
            textView.setText(showText);
            clearData();//归零
            Log.d("MyOnClick", "result=" + result + ",firstNum=" + firstNum + ",operator=" + operator);
        } else if (resid == R.id.sin) {//sin
            if (firstNum.length() == 0) {
                Toast.makeText(getApplicationContext(), "请输入数字", Toast.LENGTH_SHORT).show();
                return;
            }
            result = String.valueOf(Math.sin(Double.parseDouble(firstNum)));
            firstNum = result;
            nextNum = "";
            operator = inputText;
            showText = "sin(" + showText + ")=" + result;
            textView.setText(showText);
            clearData();
        } else if (resid == R.id.cos) {//cos
            if (firstNum.length() == 0) {
                Toast.makeText(getApplicationContext(), "请输入数字", Toast.LENGTH_SHORT).show();
                return;
            }
            result = String.valueOf(Math.cos(Double.parseDouble(firstNum)));
            firstNum = result;
            nextNum = "";
            operator = inputText;
            showText = "cos(" + showText + ")=" + result;
            textView.setText(showText);
            clearData();
        } else if (resid == R.id.tan) {//tan
            if (firstNum.length() == 0) {
                Toast.makeText(getApplicationContext(), "请输入数字", Toast.LENGTH_SHORT).show();
                return;
            }
            result = String.valueOf(Math.tan(Double.parseDouble(firstNum)));
            firstNum = result;
            nextNum = "";
            operator = inputText;
            showText = "tan(" + showText + ")=" + result;
            textView.setText(showText);
            clearData();
        } else {
            if (operator.equals("=")) {
                operator = "";
                firstNum = "";
                showText = "";

            }
            if (resid == R.id.dot) {
                if (showText.isEmpty()) {//没有就补零
                    inputText = "0.";
                }
                else if (".".equals(showText.substring(showText.length() - 1, showText.length()))) { //重复点操作
                    Toast.makeText(getApplicationContext(), "别点啦,再点人都傻了" , Toast.LENGTH_SHORT).show();
                    return;
                }
                else {
                    inputText = ".";
                }

            }
            if (operator.equals("")) {
                firstNum = firstNum + inputText;//小数处理
            } else {
                nextNum = nextNum + inputText;
            }
            showText = showText + inputText;
            textView.setText(showText);
        }


    }

    private String operator = "";//操作符
    private String firstNum = "";//前一个操作符
    private String nextNum = "";//下一个操作符
    private String result = "";//当前结果
    private String showText = "";//显示文本内容

    private boolean clear(String text) {
        showText = text;
        textView.setText(showText);
        clearData();
        return true;
    }

    private boolean calculate() {
        //分析操作符进行操作
        switch (operator) {
            case "+":
                result = String.valueOf(Arith.add(Double.parseDouble(firstNum), Double.parseDouble(nextNum)));
                break;
            case "-":
                result = String.valueOf(Arith.sub(Double.parseDouble(firstNum), Double.parseDouble(nextNum)));
                break;
            case "×":
                result = String.valueOf(Arith.mul(Double.parseDouble(firstNum), Double.parseDouble(nextNum)));
                break;
            case "÷":
                if (nextNum.equals("0")) {
                    Toast.makeText(getApplicationContext(), "你家除数能是0?", Toast.LENGTH_SHORT).show();
                    return false;
                }
                result = String.valueOf(Arith.div(Double.parseDouble(firstNum), Double.parseDouble(nextNum)));
                break;
        }
        //运算后传递
        firstNum = result;
        nextNum = "";
        return true;
    }

    private void clearData() {//清楚计算数据
        firstNum = "";
        nextNum = "";
        showText = "";
        operator = "";
        result = "";
    }
}